<?php
class Form extends CI_Controller{
	public function index()
	{
		$this->load->helper(array('form', 'url'));
		$this->load->library(array('form_validation'));

		//$this->output->cache(10);//缓存当前的view

//		$this->db->limit(10,320)->get_compiled_select('member', FALSE);
//		echo $this->db->select('title, content, date')->get_compiled_select();

		//验证方法可以单独使用
//		$this->form_validation->valid_email();


		//$this->load->database();
//		$data = array(
//				'username' => 'johndoe',
//				'password' => 'mypassword',
//				'passconf' => 'mypassword',
//				'email' => 'ssss'
//		);
//		$this->form_validation->set_data($data);
//		$this->form_validation->set_error_delimiters('<div class="error">', '</div>');

//		$this->form_validation->set_rules('username', '用户名', 'required|min_length[5]|max_length[12]|callback_username_check',
//				array(
//						'required'=>'用户名不能为空',
//						//'min_length'=>'长度不能小于5',
//						'max_length'=>'长度不能超过12',
//				));
//		$this->form_validation->set_rules('password', '密码', 'required',
//				array('required' => 'You must provide a %s.')
//		);
//		$this->form_validation->set_rules('passconf', 'Password Confirmation', 'required|matches[password]',array(
//			"matches"=>"两次密码输入不一样"
//		));
//		$this->form_validation->set_rules('email', 'Email',array(
//				'required',
//				array(
//					'email_callable',//自定义规则，需要定义一个规则名称，以供错误信息调用
//					function($str){
//						return true;
//					}
//				),
//		),array("required"=>"必须填写邮箱","email_callable"=>"{field}没有通过自定义规则email_callable的验证"));

		if ($this->form_validation->run("signup") == FALSE)
        {
            $this->load->view('myform');
//			$a = validation_errors();
//			$s = explode("\n",$a);
        }
        else
        {
            $this->load->view('formsuccess');
        }
	}
	public function username_check($str)
	{
		if ($str == 'test')
		{
			$this->form_validation->set_message('username_check', 'The {field} field can not be the word "test"');
			return FALSE;
		}
		else
		{
			return TRUE;
		}
	}

}

?>